//
//  SchoolDetailViewModel.swift
//  HoneyBee
//
//  Created by point on 2018/6/2.
//  Copyright © 2018年 point. All rights reserved.
//

import UIKit
import SwiftyJSON

class SchoolDetailViewModel: BaseViewModel {
    var model = Observable(SchoolDetailModel())
    
    var currentModel = SchoolDetailModel()
     var page: Int = 1 //当前的页数
    
    func loadData(_ str: String) {
        let uid = String(UserInfo.shareInstance.userModel().id)
        TempNetTool.get(kHost + "/institution/getInstitutionInfo?id=\(str)&timestamp=123&sign=123&operatorId=\(uid)&userId=\(uid)", parameters: nil, progress: nil, success: { [weak self] (result) in
            
            TempNetTool.get( kHost + "/comment/queryCommentForInstitutionList?id=\(str)&pageNo=1&pageSize=10&timestamp=111&sign=111", parameters: nil, progress: nil, success: { [weak self](commentResult) in
                
                
                guard let resultDict = result as? [String : NSObject] else { return }
                guard let data = resultDict["data"] as? [String : NSObject] else { return }
                self?.currentModel = SchoolDetailModel.tmSpeelBreak(data)
                
                for model:SchoolDetailResourceModel in (self?.currentModel.schoolDetailResourceModel)! {
                    if model.fileType == 1 {
                        model.path =  model.path + "?vframe/jpg/offset/1"
                    }
                    print("ssbb",model.path)
                }
                
                let json = JSON(commentResult ?? "")
                var arr:[SchoolDetailCommentModel] = [SchoolDetailCommentModel]()
                let resourceArray = json["data"].arrayValue
                for resourceJson in resourceArray{
                    let value = SchoolDetailCommentModel(fromJson: resourceJson)
                    arr.append(value)
                }
                self?.currentModel.commentArr = arr
                
                
                self?.page = 2
                self?.status = .ok
                self?.model.value = (self?.currentModel)!
                
                
            }) { [weak self] (error) in
                self?.page = 1
                self?.status = .error
                self?.model.value = (self?.currentModel)!
            }
            
        }) { [weak self] (error) in
            self?.page = 1
            self?.status = .error
            self?.model.value = (self?.currentModel)!
        }
    }
    
    
    func commentLoadDataMore(_ str: String) {
        let page = String(self.page)
        TempNetTool.get( kHost + "/comment/queryCommentForInstitutionList?id=\(str)&pageNo=\(page)&pageSize=10&timestamp=111&sign=111", parameters: nil, progress: nil, success: { [weak self](result) in
            self?.page =  (self?.page)! + 1
            let json = JSON(result ?? "")
            let resourceArray = json["data"].arrayValue
            if resourceArray.count > 0 {
                
                var arr:[SchoolDetailCommentModel] = [SchoolDetailCommentModel]()
                
                for resourceJson in resourceArray{
                    let value = SchoolDetailCommentModel(fromJson: resourceJson)
                    arr.append(value)
                }
                self?.currentModel.commentArr.append(contentsOf: arr)
                self?.status = .more
                self?.model.value = (self?.currentModel)!
                
            }else {
                self?.status = .done
                self?.model.value = (self?.currentModel)!
            }
        }) { (error) in
            
        }
        
    }
    
    
    
   
    
    
}
